package ch.bfh.medinfo.sendingsms;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class SendSMS extends Activity {
	private Button sendButton;
	private Spinner spinner;
	private EditText smsBodyEditText;
	private EditText smsAddressEditText;

	// Some 'ugly' flags...
	private boolean sendableText = false;
	private boolean sendableAddress = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_send_sms);
		sendButton = (Button) findViewById(R.id.button1);
		spinner = (Spinner) findViewById(R.id.spinner1);
		smsBodyEditText = (EditText) findViewById(R.id.editText1);
		smsAddressEditText = (EditText) findViewById(R.id.editText2);
		sendButton.setOnClickListener(new SendSMSManager());
		smsBodyEditText.setOnKeyListener(new SMSBodyEditManager());
		smsAddressEditText.setOnKeyListener(new SMSAddressEditManager());
		checkSendEndable();
	}

	@Override
	protected void onStart() {
		super.onStart();

	}

	/**
	 * checks if the Send-Button can be enabled.
	 */
	protected void checkSendEndable() {
		sendButton.setEnabled(sendableAddress && sendableText);
	}
	


	// ///////////////////////////////////////7

	/**
	 * This inner class manages the Send-'Button' and SMS-Sending
	 * 
	 * @author reto
	 */
	class SendSMSManager implements OnClickListener {
		@Override
		public void onClick(View v) {
			String language = (String) spinner.getSelectedItem();
			language = language.toUpperCase();
			String smsBody = smsBodyEditText.getText().toString();
			smsBody = language + smsBody;
			String smsAddress = smsAddressEditText.getText().toString();
			Log.i("ch.bfh.medinfo", "smsAddress: " + smsAddress + " smsBody: "
					+ smsBody);

			// SMS-Sending (Do not forget to set the permissions in the
			// manifest)
			SmsManager smsManager = SmsManager.getDefault();
			smsManager.sendTextMessage(smsAddress, null, smsBody, null, null);
		}
	}

	/**
	 * This inner class manages the Content of the SMS-Body (Not too big, not
	 * too small)
	 * 
	 * @author reto
	 * 
	 */
	class SMSBodyEditManager implements OnKeyListener {

		@Override
		public boolean onKey(View v, int keyCode, KeyEvent event) {
			int textLength = ((TextView) v).getText().length();
			if (textLength < 1 || textLength > 100)
				sendableText = false;
			else
				sendableText = true;
			checkSendEndable();
			return false;
		}
	}

	/**
	 * This inner class manages the content of the SMS-Address (Not empty and
	 * not too many)
	 * 
	 * @author reto
	 * 
	 */
	class SMSAddressEditManager implements OnKeyListener {

		@Override
		public boolean onKey(View v, int keyCode, KeyEvent event) {
			int textLength = ((TextView) v).getText().length();
			if (textLength < 1 || textLength > 15)
				sendableAddress = false;
			else
				sendableAddress = true;
			checkSendEndable();
			return false;
		}
	}

}
